const app = getApp()
Page({
  data: {
    images: [],
    currentIndex: -1
  },
  onLoad(){ 
    this.requsert();
  },
  requsert:function(){
    let that = this
    app.util.request({
      url: 'entry/wxapp/ShartBg',
      success(res) {
        let images = res.data
        that.setData({ images })
      }
    })
  },
  showImg(e){
    let url = e.currentTarget.dataset.url + '?' + (+ new Date())
    wx.previewImage({
      urls: [url],
    })
  },
  selectImg(e){
    this.img_id = e.currentTarget.dataset.id
    this.setData({currentIndex: this.img_id})
  },
  download(){
    let user_id = wx.getStorageSync('users').id
    let img_id = this.img_id
    if(!img_id) {
      wx.showToast({
        title: '请选择背景图',
        icon: 'none'
      })
      return
    }
    wx.showLoading({
      title: '下载中...',
      mask: true
    })
    app.util.request({
      url: 'entry/wxapp/UserBg',
      data: {
        user_id,
        img_id
      },
      success(res) {
        wx.downloadFile({
          url: res.data.qrcode,
          success(res){
            wx.saveImageToPhotosAlbum({
              filePath: res.tempFilePath,
              success(res){
                wx.hideLoading()
                // app.util.request({
                //   url: "entry/wxapp/INsetHBB",
                //   data: {
                //     user_id,
                //   },
                //   success(res){
                //     wx.vibrateLong({})
                //     wx.playBackgroundAudio({
                //       dataUrl: 'https://tc.wsmo.cn/we7/addons/zh_tcwq/weapp_img/bgs/money.mp3'
                //     })
                //     wx.showToast({
                //       icon: 'none',
                //       title: '恭喜！你获得10个红包币',
                //       duration: 3000
                //     })
                //   }
                // })
              }
            })
          }
        })
      }
    })
  }
})